﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TireService.Data
{
    public class CarBLL
    {
        public void UpdateCar(TireServiceDB db, Car car, CustomerSubscription customersubscription, Customer customer, int CarId, string RegistrationNumber, int CarBrandId, string Model, string EngineSize, int FuelType, int Year, string Color, int CarStatusId, DateTime? LastTireSetChangeDate, DateTime? NextTireSetChangePlanningDate, bool SubmitInTransaction)
        {
            try
            {
                // Check if existing car is being updated
                if (CarId != 0)
                {
                    car = db.GetCar(CarId);
                }

                // Create/Update existing car
                customer.Cars.Add(car);
                customersubscription.Cars.Add(car);
                car.RegistrationNumber = RegistrationNumber;
                car.CarBrandId = CarBrandId;
                car.Model = Model;
                car.EngineSize = EngineSize;
                car.FuelType = FuelType;
                car.Year = Year;
                car.Color = Color;
                car.CarStatusId = CarStatusId;
                if (LastTireSetChangeDate != null) car.LastTireSetChangeDate = Convert.ToDateTime(LastTireSetChangeDate);
                if (NextTireSetChangePlanningDate != null) car.NextTireSetChangePlanningDate = Convert.ToDateTime(NextTireSetChangePlanningDate);

                // Create new car
                if (CarId == 0)
                {
                    db.AddCar(car);
                }

                // Check if car is being created/updated in other transaction
                if (!SubmitInTransaction)
                {
                    // Submit changes to database
                    db.SubmitChanges();
                }
            }
            catch (Exception ex)
            {


            }

        }
    }
}
